package com.neuedu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.pojo.Expend;
import com.neuedu.pojo.Income;
import com.neuedu.pojo.Type;
import com.neuedu.service.IExpendService;
import com.neuedu.service.IIncomeService;
import com.neuedu.service.ITypeService;
import com.neuedu.util.CommonResult;
import com.neuedu.util.SearchDTO;
import com.neuedu.util.SumDTO;
import com.neuedu.util.YearDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sum")
public class SumController {
    @Resource
    IExpendService expendService;
    @Resource
    IIncomeService incomeService;
    @Resource
    ITypeService typeService;

    @PostMapping("list")
    CommonResult list(SearchDTO s){
        System.out.println("输出");
        System.out.println(s);
        List<SearchDTO> list =new ArrayList<>(10);
        QueryWrapper<Income> in =new QueryWrapper<>();
        QueryWrapper<Expend> ex =new QueryWrapper<>();
        in.eq("active",1);
        ex.eq("active",1);
        if(!StringUtils.isBlank(s.getItem())){
            if("全部".equals(s.getQueryType())||"收入".equals(s.getQueryType()))
                in.like("item",s.getItem());
            if("全部".equals(s.getQueryType())||"支出".equals(s.getQueryType()))
                ex.like("item_name",s.getItem());
        }
        if(s.getDateRange()!=null&&s.getDateRange().length>0&&s.getDateRange()[0]!=null){
            if("全部".equals(s.getQueryType())||"收入".equals(s.getQueryType()))
                in.between("income_date",s.getDateRange()[0],s.getDateRange()[1]);
            if("全部".equals(s.getQueryType())||"支出".equals(s.getQueryType()))
                ex.between("out_date",s.getDateRange()[0],s.getDateRange()[1]);
        }
        if("全部".equals(s.getQueryType())||"收入".equals(s.getQueryType())){
            List<Income> inlist = incomeService.list(in);
            for(Income i:inlist){
                SearchDTO dto =new SearchDTO();
                dto.setItem(i.getItem());
                dto.setQueryType("收入");
                dto.setDate(i.getIncomeDate());
                dto.setSum(i.getSum());
                dto.setUse(i.getDonator());
                list.add(dto);
            }
        }
        if("全部".equals(s.getQueryType())||"支出".equals(s.getQueryType())){
            List<Expend> exlist=expendService.list(ex);
            for(Expend e:exlist){
                SearchDTO dto =new SearchDTO();
                dto.setItem(e.getItemName());
                dto.setQueryType("支出");
                dto.setDate(e.getOutDate());
                dto.setSum(e.getOutSum());
                dto.setUse(e.getUseText());
                list.add(dto);
            }
        }

        return CommonResult.success(list);
    }

    @PostMapping("sum")
    CommonResult sum(String[] dateRange) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date1 = sdf.parse(dateRange[0]);
        Date date2 = sdf.parse(dateRange[1]);
        System.out.println("-------------------------------");
        QueryWrapper<Income> qu=new QueryWrapper<>();
        qu.between("income_date",date1,date2);
        qu.eq("active",1);
        List<Income> list=incomeService.list(qu);
        BigDecimal[] barr = new BigDecimal[20];//下标为类型的id,用来存统计的结果。

        for(int i=0;i<barr.length;i++)
            barr[i]=new BigDecimal(0);

        BigDecimal all=new BigDecimal(0);//全部的金额
        for(Income i:list){
            barr[i.getType()]= barr[i.getType()].add(i.getSum());//加上钱
            all=all.add(i.getSum());
        }
        //获取所有的类型
        QueryWrapper<Type> qut=new QueryWrapper<>();
        qut.eq("active",1);
        List<Type> tl = typeService.list(qut);

        List<SumDTO> l=new ArrayList<>(15);
        for(int i=0;i<barr.length;i++){
            if(barr[i]!=null&&barr[i].compareTo(new BigDecimal(0))==1){
                SumDTO dto=new SumDTO();
                for(Type t:tl){  //遍历类型表，把id转成对应的name
                    if(t.getId()==i){
                        dto.setName(t.getTypeName());
                        break;
                    }
                }
                dto.setSum(barr[i]);
                dto.setValue(barr[i].divide(all,2,BigDecimal.ROUND_HALF_UP));//做除法，四舍五入，求各自的占比
                l.add(dto);
            }
        }
        return CommonResult.success(l);

    }

    @GetMapping("year")
    CommonResult year(String date1,String date2){
        Integer d1=Integer.parseInt(date1);
        Integer d2=Integer.parseInt(date2);
        if(d1>=d2){
            return CommonResult.failed();
        }
        List<BigDecimal> arr =new ArrayList();
        List<String> years=new ArrayList<>();
        BigDecimal all=new BigDecimal(0);
        QueryWrapper<Income> qu;
        List<Income> l;
        for(int i=d1;i<d2;i++){
            qu=new QueryWrapper<>();
            qu.eq("active",1);
            qu.like("income_date",i+"");
            l=incomeService.list(qu);
            BigDecimal b=new BigDecimal(0);
            for(Income in:l){
                b=b.add(in.getSum());
            }
            years.add(i+"");
            arr.add(b);
            all=all.add(b);
        }
        //反顺序的操作！！！！！！！！不要模仿
        List<String> years2=new ArrayList<>();
        for(int i=years.size()-1;i>=0;i--){
            years2.add(years.get(i));
        }
        List<BigDecimal> arr2 =new ArrayList();
        for(int i=arr.size()-1;i>=0;i--){
            arr2.add(arr.get(i));
        }

        YearDTO dto=new YearDTO();
        dto.setAll(all);
        dto.setAve(all.divide(new BigDecimal(d2-d1),2,BigDecimal.ROUND_HALF_UP));
        dto.setArr(arr2);
        dto.setYears(years2);
        return CommonResult.success(dto);
    }
}
